import { Inject, Injectable } from '@nestjs/common';
import { PrismaService } from '@/prisma/prisma.service';

@Injectable()
export class PaginationService {
  @Inject(PrismaService)
  private readonly prisma: PrismaService;

  async paginate(
    table: string,
    query: object = {},
    pageNum: number = 1,
    pageSize: number = 999999,
  ) {
    const offset = (pageNum - 1) * pageSize;
    const [list, total] = await Promise.all([
      this.prisma[table].findMany({
        ...query,
        skip: offset,
        take: pageSize,
      }),
      this.prisma[table].count({
        where: query['where'],
      }),
    ]);
    return { total, list };
  }
}
